package com.qf.json;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qf.pickname.entity.Student;

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) throws JsonProcessingException {
        Student student = new Student(100,"luffy","男");

        // 对象 ---》 JSON
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(student);
        System.out.println(json);

        // JSON ---》对象
        String s = "{\"id\":200,\"name\":\"luffy1\",\"sex\":\"男\",\"checked\":1}";
        Student luffy = objectMapper.readValue(s, Student.class);
        System.out.println(luffy);

        // 集合 转换为 json
        ArrayList<Student> students = new ArrayList<>();
        students.add(student);
        students.add(luffy);

        String json1 = objectMapper.writeValueAsString(students);
        System.out.println(json1);
    }
}
